package im.jsmith.android.bloxbreaker;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.animator.AlphaMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;

import com.scoreninja.adapter.ScoreNinjaAdapter;

/**
 * @author Jordon Smith
 */
public class MenuActivity extends BaseGameActivity implements GameConstants, IOnMenuItemClickListener {
	private Camera mCamera;
	private Scene mScene;

	private Texture mFontTex;
	private Font mFont;
	
	private Sound mSelectSound;
	
	private ScoreNinjaAdapter mScoreNinjaAdapter;
	
	@Override
	public Engine onLoadEngine() {
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera).setNeedsSound(true));
	}

	@Override
	public void onLoadResources() {
		mFontTex = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mFont = new Font(mFontTex, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 60, true, Color.WHITE);
		
		mEngine.getFontManager().loadFont(mFont);
		mEngine.getTextureManager().loadTextures(mFontTex);
	}

	@Override
	public Scene onLoadScene() {
		mScene = new Scene(1);
		initSounds();
		mScene.setBackground(new ColorBackground(0, 0, 0));
		mScene.setChildScene(createMenuScene());
		return mScene;
	}

	@Override
	public void onLoadComplete() {
		mScoreNinjaAdapter = new ScoreNinjaAdapter(this, "bloxbreaker", "");
	}
	
	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case MENU_NEWGAME:
			mSelectSound.play();
			startActivity(new Intent(this, GameActivity.class));
			return true;
		case MENU_SETTINGS:
			mSelectSound.play();
			startActivity(new Intent(this, GamePrefs.class));
			return true;
		case MENU_GLOBALSCORES:
			mSelectSound.play();
			/* Bug: http://code.google.com/p/scoreninja/issues/detail?id=6 */
			mScoreNinjaAdapter.show(-1);
			return true;
		case MENU_EXIT:
			mSelectSound.play();
			finish();
			return true;
		}
		return false;
	}
	
	private MenuScene createMenuScene() {
		final MenuScene menuScene = new MenuScene(mCamera);
		menuScene.setMenuAnimator(new AlphaMenuAnimator(8));
		
		final IMenuItem newGameItem = new TextMenuItem(MENU_NEWGAME, mFont, "NEW GAME");
		menuScene.addMenuItem(newGameItem);
		
		final IMenuItem globalScoresItem = new TextMenuItem(MENU_GLOBALSCORES, mFont, "GLOBAL SCORES");
		menuScene.addMenuItem(globalScoresItem);
		
		final IMenuItem localScoresItem = new TextMenuItem(MENU_SETTINGS, mFont, "SETTINGS");
		menuScene.addMenuItem(localScoresItem);
		
		final IMenuItem exitItem = new TextMenuItem(MENU_EXIT, mFont, "EXIT");
		menuScene.addMenuItem(exitItem);
		
		menuScene.buildAnimations();
		menuScene.setBackgroundEnabled(false);
		menuScene.setOnMenuItemClickListener(this);
		return menuScene;
	}
	
	private void initSounds() {
		try {
			mSelectSound = SoundFactory.createSoundFromAsset(mEngine.getSoundManager(), this, "wav/select.wav");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}